﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Windows.Forms.DataVisualization.Charting;



namespace Chart
{
    public partial class graph : UserControl
    {
        int z = 0;
        public string ticker = "bumi.jk";
        int monthStart;
        int dateStart;
        int yearStart;
        int monthEnd;
        int dateEnd;
        int yearEnd;
        double min;
        double max;
        public List<DateTime> date = new List<DateTime>();
        public List<double> Close = new List<double>();
        public List<double> Open = new List<double>();
        public List<double> High = new List<double>();
        public List<double> Low = new List<double>();
        double[] Gain = new double[14];
        double[] Loss = new double[14];
        double[] totalGain = new double[14];
        double[] totalLoss = new double[14];
        double[] avgGain = new double[7];
        double[] avgLoss = new double[7];
        double[] RS = new double[7];
        public double[] RSI = new double[7];
        double[] currentGain = new double[7];
        double[] currentLoss = new double[7];
        public string status = "";
        double predict;

        List<Stock> data;

        public string prediction()
        {
            if (RSI[0] < 80 && RSI[0] > 20)
            {
                status = "stagnant";
                return status;
            }
            else if (RSI[0] > 80)
            {
                status = "down";
                return status;
            }
            else
            {
                status = "up";
                return status;
            }
        }
        public graph()
        {
            InitializeComponent();
        }

        public Color XAxisLabelColor
        {
            get
            {
                return chart1.ChartAreas[0].AxisX.LabelStyle.ForeColor;
            }
            set
            {
                chart1.ChartAreas[0].AxisX.LabelStyle.ForeColor = value;
            }
        }

        public Color YAxisLabelColor
        {
            get
            {
                return chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor;
            }
            set
            {
                chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor = value;
            }
        }

        public int XAxisWidth
        {
            get
            {
                return chart1.ChartAreas[0].AxisX.LineWidth;
            }
            set
            {
                chart1.ChartAreas[0].AxisX.LineWidth = value;
            }
        }

        public int YAxisWidth
        {
            get
            {
                return chart1.ChartAreas[0].AxisY.LineWidth;
            }
            set
            {
                chart1.ChartAreas[0].AxisY.LineWidth = value;
            }
        }

        public Color XAxisColor
        {
            get
            {
                return chart1.ChartAreas[0].AxisX.LineColor;
            }
            set
            {
                chart1.ChartAreas[0].AxisX.LineColor = value;
            }
        }
        public Color YAxisColor
        {
            get
            {

                return chart1.ChartAreas[0].AxisY.LineColor;
            }
            set
            {
                chart1.ChartAreas[0].AxisY.LineColor = value;
            }
        }

        public Color VerticalLineColor
        {
            get
            {
                return chart1.ChartAreas[0].AxisX.MajorGrid.LineColor; ;
            }
            set
            {
                chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = value;
            }
        }

        public Color HorizontalLineColor
        {
            get
            {
                return chart1.ChartAreas[0].AxisY.MajorGrid.LineColor; ;
            }
            set
            {
                chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = value;
            }
        }

        public int VerticalLineSize
        {
            get
            {
                return chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth;
            }
            set
            {
                chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = value;
            }
        }

        public int HorizontalLineSize
        {
            get
            {
                return chart1.ChartAreas[0].AxisY.MajorGrid.LineWidth;
            }
            set
            {
                chart1.ChartAreas[0].AxisY.MajorGrid.LineWidth = value;
            }
        }

        public Color HighLowLine
        {
            get
            {
                return chart1.Series[0].Color;
            }
            set
            {
                chart1.Series[0].Color = value;
            }
        }


        public Color GraphBackColor
        {
            get
            {
                return chart1.ChartAreas[0].BackColor;
            }
            set
            {
                chart1.ChartAreas[0].BackColor = value;
            }
        }

        public GradientStyle GraphBackGradient
        {
            get
            {
                return chart1.ChartAreas[0].BackGradientStyle;
            }
            set
            {
                chart1.ChartAreas[0].BackGradientStyle = value;
            }
        }

        public Color GraphBackColor2
        {
            get
            {
                return chart1.BackColor;
            }
            set
            {
                chart1.BackColor = value;
            }
        }

        public Color titleColor
        {
            get
            {
                return chart1.Titles[0].ForeColor;
            }
            set
            {
                chart1.Titles[0].ForeColor = value;
            }
        }

        public Font titleFontStyle
        {
            get
            {
                return chart1.Titles[0].Font;
            }
            set
            {
                chart1.Titles[0].Font = value;
            }
        }

        public Font XAxisFontStyle
        {
            get
            {
                return chart1.ChartAreas[0].AxisX.LabelStyle.Font;
            }
            set
            {
                chart1.ChartAreas[0].AxisX.LabelStyle.Font = value;
            }
        }

        public Font YAxisFontStyle
        {
            get
            {
                return chart1.ChartAreas[0].AxisY.LabelStyle.Font;
            }
            set
            {
                chart1.ChartAreas[0].AxisY.LabelStyle.Font = value;
            }
        }

        public ChartHatchStyle GraphHatchStyle
        {
            get
            {
                return chart1.ChartAreas[0].BackHatchStyle;
            }
            set
            {
                chart1.ChartAreas[0].BackHatchStyle = value;
            }
        }

        public void tampil()
        {
            DateTime date2 = date[z-1];
            Double x = date2.ToOADate();
            chart1.Titles[0].Text = ticker;

            chart1.Series[0].XValueMember = "Date";
            chart1.Series[0].XValueType = ChartValueType.DateTime;
            //chart1.ChartAreas[0].AxisX.Minimum = x - 1;
            //chart1.ChartAreas[0].AxisX.Maximum = d + 7;
            chart1.ChartAreas[0].AxisY.Minimum = min - 30;
            chart1.ChartAreas[0].AxisY.Maximum = max + 30;

            int k = z - 1;
            //MessageBox.Show(z.ToString());
            for (int i = -1; i < 6; i++)
            {
                if (i == -1)
                    continue;
                if (i == 6)
                    break;

                chart1.Series[0].Points.AddXY(date[i], High[i], Low[i], Open[i], Close[i]);
            }

            
        }

        public void tampil2()
        {
            for (int j = 20; j > 6; j--)
            {
                
                if (Close[j + 1] - Close[j] > 0)
                {
                    Gain[0] = 0;
                    Loss[0] = Close[j + 1] - Close[j];
                }
                else if (Close[j] - Close[j + 1] > 0)
                {
                    Gain[0] = Close[j] - Close[j + 1];
                    Loss[0] = 0;
                }
                else
                {
                    Gain[0] = 0;
                    Loss[0] = 0;
                }

                totalGain[6] += Gain[0];
                totalLoss[6] += Loss[0];
                
            }
            avgGain[6] = totalGain[6] / 14;
            avgLoss[6] = totalLoss[6] / 14;
            RS[6] = avgGain[6] / avgLoss[6];
            RSI[6] = 100 - (100 * (1 / (1 + RS[6])));
            for (int a = 5;a >= 0;a--)//a semakin besar paling lama, semakin kecil semakin baru (mendekati hari ini)
            {
                int counter = 0;
               
                    
                    if (Close[a + 1] - Close[a] > 0)
                    {
                        Gain[counter] = 0;
                        Loss[counter] = Close[a + 1] - Close[a];
                       
                            currentLoss[a] = Loss[counter];
                        
                    }
                    else if (Close[a] - Close[a + 1] > 0)
                    {
                        Gain[counter] = Close[a] - Close[a + 1];
                        Loss[counter] = 0;
                       
                            currentGain[a] = Gain[counter];
                        
                    }
                    else
                    {
                        Gain[counter] = 0;
                        Loss[counter] = 0;
                       
                            currentGain[a] = Gain[counter];
                       
                    }
                    
                   // totalGain[a] += Gain[counter];
                   // totalLoss[a] += Loss[counter];
                    counter++;
 
                
                
                

                avgGain[a] = (avgGain[a + 1] * 13 + currentGain[a]) / 14;
                avgLoss[a] = (avgLoss[a + 1] * 13 + currentLoss[a]) / 14;

                RS[a] = avgGain[a] / avgLoss[a];
                RSI[a] = 100 - (100 * (1 / (1 + RS[a])));
               // MessageBox.Show(RSI[a].ToString());

                chart1.Series[1].Points.AddY(RSI[a]);
                
            }
            //

        }

        public void ambilData()
        {
            z = 0;
            monthStart = DateTime.Today.Month - 3;
            dateStart = DateTime.Today.Day;
            yearStart = DateTime.Today.Year;
            monthEnd = DateTime.Today.Month;
            dateEnd = DateTime.Today.Day - 1;
            yearEnd = DateTime.Today.Year;
            int monthCheck = monthStart - 1;

            if (dateStart < 1)
            {
                monthStart = monthCheck;
                if (monthCheck == 0 || monthCheck == 2 || monthCheck == 4 || monthCheck == 6 || monthCheck == 7 || monthCheck == 9 || monthCheck == 11)
                {
                    if (dateStart == 0)
                    {
                        dateStart = 31;
                    }
                    else if (dateStart == -1)
                    {
                        dateStart = 30;
                    }
                    else if (dateStart == -2)
                    {
                        dateStart = 29;
                    }
                    else if (dateStart == -3)
                    {
                        dateStart = 28;
                    }
                    else if (dateStart == -4)
                    {
                        dateStart = 27;
                    }
                    else if (dateStart == -5)
                    {
                        dateStart = 26;
                    }
                    else if (dateStart == -6)
                    {
                        dateStart = 25;
                    }
                }
                else if (monthCheck == 3 || monthCheck == 5 || monthCheck == 8 || monthCheck == 10)
                {
                    if (dateStart == 0)
                    {
                        dateStart = 30;
                    }
                    else if (dateStart == -1)
                    {
                        dateStart = 29;
                    }
                    else if (dateStart == -2)
                    {
                        dateStart = 28;
                    }
                    else if (dateStart == -3)
                    {
                        dateStart = 27;
                    }
                    else if (dateStart == -4)
                    {
                        dateStart = 26;
                    }
                    else if (dateStart == -5)
                    {
                        dateStart = 25;
                    }
                    else if (dateStart == -6)
                    {
                        dateStart = 24;
                    }
                }
                else if (monthCheck == 1 && yearStart % 4 == 0)
                {
                    if (dateStart == 0)
                    {
                        dateStart = 29;
                    }
                    else if (dateStart == -1)
                    {
                        dateStart = 28;
                    }
                    else if (dateStart == -2)
                    {
                        dateStart = 27;
                    }
                    else if (dateStart == -3)
                    {
                        dateStart = 26;
                    }
                    else if (dateStart == -4)
                    {
                        dateStart = 25;
                    }
                    else if (dateStart == -5)
                    {
                        dateStart = 24;
                    }
                }
                else
                {
                    if (dateStart == 0)
                    {
                        dateStart = 28;
                    }
                    else if (dateStart == -1)
                    {
                        dateStart = 27;
                    }
                    else if (dateStart == -2)
                    {
                        dateStart = 26;
                    }
                    else if (dateStart == -3)
                    {
                        dateStart = 25;
                    }
                    else if (dateStart == -4)
                    {
                        dateStart = 24;
                    }
                    else if (dateStart == -5)
                    {
                        dateStart = 23;
                    }
                }

            }
            data = StockDownloader.DownloadData(ticker, monthStart, dateStart, yearStart, monthEnd, dateEnd, yearEnd);


            foreach (Stock stock in data)
            {
                date.Add(stock.Date);
                Close.Add(stock.AdjClose);
                Open.Add(stock.Open);
                High.Add(stock.High);
                Low.Add(stock.Low);
                z++;
            }
            min = Low[0];
            for (int k = 1; k < z; k++)
            {
                if (Low[k] < min)
                    min = Low[k];
            }
            max = High[0];
            for (int k = 1; k < z; k++)
            {
                if (High[k] > max)
                    max = High[k];
            }
            chart1.Series[0].Name = ticker;
            chart1.Series[1].Name = ticker+" RSI";
        }

        public void clearChart()
        {
            date.Clear();
            Array.Clear(Gain, 0, Gain.Length);
            Array.Clear(Loss, 0, Loss.Length);
            Array.Clear(totalGain, 0, totalGain.Length);
            Array.Clear(totalLoss, 0, totalLoss.Length);
            Array.Clear(avgGain, 0, avgGain.Length);
            Array.Clear(RS, 0, RS.Length);
            Array.Clear(avgLoss, 0, avgLoss.Length);
                Array.Clear(currentGain, 0, currentGain.Length);
                Array.Clear(currentLoss, 0, currentLoss.Length);
            Array.Clear(RSI, 0, RSI.Length);
            Close.Clear();
            Open.Clear();
            High.Clear();
            Low.Clear();
            chart1.Series[0].Points.Clear();
            chart1.Series[1].Points.Clear();
            
        }

        private void graph_Load(object sender, EventArgs e)
        {
            ambilData();
            tampil();
            tampil2();
        }

        private void chart1_Click(object sender, EventArgs e)
        {

        }
    }
}
